package com.psychology.controller;

import com.psychology.dto.TherapistProfileDTO;
import com.psychology.service.TherapistService;
import com.psychology.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/therapist")
@Slf4j
public class TherapistController {

    @Autowired
    private TherapistService therapistService;

    @GetMapping("/profile")
    public Result<?> getProfile() {
        try {
            return Result.success(therapistService.getProfile());
        } catch (Exception e) {
            log.error("获取咨询师信息失败", e);
            return Result.error(e.getMessage());
        }
    }

    @PutMapping("/profile")
    public Result<?> updateProfile(@RequestBody @Valid TherapistProfileDTO profileDTO) {
        log.info("更新咨询师信息: {}", profileDTO);
        try {
            therapistService.updateProfile(profileDTO);
            return Result.success();
        } catch (Exception e) {
            log.error("更新咨询师信息失败", e);
            return Result.error(e.getMessage());
        }
    }

    @GetMapping("/schedule/today")
    public Result<?> getTodaySchedule() {
        try {
            List<AppointmentVO> schedule = therapistService.getTodaySchedule();
            return Result.success(schedule);
        } catch (Exception e) {
            log.error("获取今日日程失败", e);
            return Result.error(e.getMessage());
        }
    }

    @GetMapping("/schedule/weekly")
    public Result<?> getWeeklySchedule(@RequestParam String startDate) {
        try {
            List<ScheduleVO> schedules = therapistService.getWeeklySchedule(startDate);
            return Result.success(schedules);
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            log.error("获取周排班失败", e);
            return Result.error("获取周排班失败，请稍后重试");
        }
    }

    @PostMapping("/schedule")
    public Result<?> updateSchedule(@RequestBody @Valid ScheduleDTO scheduleDTO) {
        try {
            therapistService.updateSchedule(scheduleDTO);
            return Result.success("排班添加成功");
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            log.error("更新排班失败", e);
            return Result.error("更新排班失败，请稍后重试");
        }
    }
}